﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Infragistics.WebUI.UltraWebGrid;

public partial class Page_Good_Manager : PageBase
{
    public string treeID = "";          //选中树中节点
    public string queryID = "";         //条件查询内容

    private static DataTable dt = new DataTable();

    protected void Page_Load(object sender, EventArgs e)
    {
        // 记录SiteID,UltraWebGrid控件分页时_Site.ID的值会丢失。
        if (_Site != null)
        {
            txtSiteID.Text = _Customer.ShopID.ToString();
        }

        this.RegisterJS();

        if (!IsPostBack)
        {
            Utility.InitUltraWebGrid.InitDisplayLayOut(this.UWGGood);

            this.ReadAction();
            this.LoadInitTree();
            this.LoadGoods("");
        }
    }

    #region Web 窗体设计器生成的代码

    protected override void OnLoad(EventArgs e)
    {
        isRequestLogin = true;                                  //是否需要登录
        RequestLoginPage = this.Request.Url.AbsoluteUri;        //获取决对Url
        isAtFramePageLogin = true;                              //是框架页

        //        RequestCompetences = Competences.BuildCompetencesList(Competences.DAGL_71);//页面权限

        base.OnLoad(e);
    }

    #endregion

    protected void TVGoodType_SelectedNodeChanged(object sender, EventArgs e)
    {
        treeID = TVGoodType.SelectedNode.Value.ToString();           //商品类别编号
        ViewState["treeID"] = treeID;
        LoadGoods(treeID);
    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        Response.Redirect("Good_Edit.aspx?Action=Add");
    }

    protected void btnEdit_Click(object sender, EventArgs e)
    {
        string ID = Hidden1.Value;
        string GoodTypeID = new DAL.Tables.tbl_Shop_Good().Open("", "ID=" + ID, "").Rows[0]["GoodTypeID"].ToString();

        if (!ID.Equals(""))
        {
            Response.Redirect("Good_Edit.aspx?Action=Edit&GoodID=" + ID + "&GoodTypeID=" + GoodTypeID);
        }
    }

    protected void btnDel_Click(object sender, EventArgs e)
    {
        string ID = Hidden1.Value;

        if (!ID.Equals(""))
        {
            long i = new DAL.Tables.tbl_Shop_Good().Delete("[ID]=" + ID);

            if (i >= 0)
            {
                Shove._Web.JavaScript.Alert(this.Page, "删除成功！");
            }
            else
            {
                Shove._Web.JavaScript.Alert(this.Page, "删除失败！");
            }

            this.LoadGoods("");
        }

        this.Hidden1.Value = "";
    }

    protected void btnExcel_Click(object sender, EventArgs e)
    {
        Utility.DataTableToExcel.DataTableToExcelAndDownload(dt, "Good_Manager.xls");
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        string condition = txtCondition.Text;
        ViewState["condition"] = condition;

        this.searchQuery();
    }

    protected void UWGGood_PageIndexChanged(object sender, Infragistics.WebUI.UltraWebGrid.PageEventArgs e)
    {
        string tId = (string)ViewState["treeID"];
        string sId = (string)ViewState["condition"];

        if (tId != "")           //树节点查询
        {
            this.LoadGoods(tId);
            ViewState["queryID"] = "";            //置空
        }
        else if (sId != "")     //条件查询
        {
            this.searchQuery();
        }
        else
        {
            this.LoadGoods("");     //无条件，查询出所有
        }
    }

    private void ReadAction()
    {
        string sAction = Shove._Web.Utility.GetRequest("Type");
        if (sAction == "Shop")
        {
            trButtons.Visible = false;
        }
    }

    private void RegisterJS()
    {
        Infragistics.WebUI.Shared.Util.ClientScript.ResolveAndRegisterClientScriptInclude(
             Shove._Web.Utility.GetUrl() + "/js/Scripts/ig_webgrid_an.js",
             "",
             Shove._Web.Utility.GetUrl() + "/js/Scripts/ig_webgrid_an.js",
             "ig_webgrid_an.js", this.UWGGood, "ig_webgrid_an");          //注册 Infragistics js和控件        
    }

    /// <summary>
    /// 条件查询
    /// </summary>
    protected void searchQuery()
    {
        string tempCondition = (string)ViewState["condition"];
        DataTable dt = new DataTable();

        string Condtion = "" + ddlType.SelectedValue + " like '%" + tempCondition + "%'" + " and (ShopID=" + _Customer.SiteID + ") ";
        dt = new DAL.Views.V_Shop_Good().Open("", Shove._Web.Utility.FilteSqlInfusion(Condtion), "");
        this.UWGGood.DataSource = dt;

        this.UWGGood.DataBind();
    }

    /// <summary>
    /// 梆定所有商品信息
    /// </summary>
    private void LoadGoods(string GoodTypeID)
    {
        if (dt != null && dt.Rows.Count > 0)
        {
            dt.Clear();
        }

        long SiteID = Shove._Convert.StrToLong(txtSiteID.Text, -1);

        string Fields = "[ID],[GoodNo],(select BrandName_CN from tbl_Shop_Brand where [ID] = BrandID)as BrandName,GoodName_CN,GoodName_EN,Price_Orig,Price_Vip,Price_BV,Price_Lastin,GoodNorm,GoodUnit,GoodColor,GoodIntroduce,GoodPic_Big";
        string Condition = "(ShopID=" + SiteID + ") ";
        string Sort = "";

        if ((GoodTypeID != "") && (GoodTypeID != null))
        {
            Condition = "(ShopID=" + SiteID + ") and (substring(GoodTypeID,1,len('" + GoodTypeID + "')))='" + GoodTypeID + "'";
        }

        dt = new DAL.Tables.tbl_Shop_Good().Open(Fields, Condition, Sort);

        UWGGood.DataSource = dt;
        UWGGood.DataBind();
    }

    /// <summary>
    /// 加载商品类别信息到树中
    /// </summary>
    private void LoadInitTree()
    {
        string ID = "";

        string Fields = "ID,TypeName,";
        Fields += "(case when TypeStatus ='1' then '正常' else '停用' end )as TypeStatus,ParentID,Introduce";
        string Condition = ""; //"(ShopID=" + _Site.ID + ")";
        string Sort = "";

        if (!ID.Equals(""))
        {
            Condition = "(substring(ID,1,len('" + ID + "')))='" + ID + "'"; //(ShopID=" + _Site.ID + ")  and 
        }

        DataTable dt = new DAL.Tables.tbl_Shop_GoodType().Open(Fields, Condition, Sort);

        DataView dv = new DataView(dt);
        dv.RowFilter = "ParentID='00'";

        //添加一根节点
        TreeNode node1 = new TreeNode();
        node1.Text = "所有类别";
        node1.Value = "00";
        TVGoodType.Nodes.Add(node1);

        foreach (DataRowView drv in dv)
        {
            TreeNode node = new TreeNode();
            node.Text = drv["TypeName"].ToString();
            node.Value = drv["ID"].ToString();
            node.Expanded = false;
            node1.ChildNodes.Add(node);
            AddReplies(dt, node);
        }
    }

    /// <summary>
    /// 递归绑定商品类别到树中 
    /// </summary>
    private void AddReplies(DataTable dt, TreeNode node)
    {
        DataView dv = new DataView(dt);
        dv.RowFilter = "ParentID='" + node.Value + "'";

        foreach (DataRowView row in dv)
        {
            TreeNode replyNode = new TreeNode();
            replyNode.Text = row["TypeName"].ToString();
            replyNode.Value = row["ID"].ToString();
            replyNode.Expanded = false;
            node.ChildNodes.Add(replyNode);
            AddReplies(dt, replyNode);
        }
    }

    /// <summary>
    /// 商品图片列表
    /// </summary>
    protected void btnGoodPic_Click(object sender, EventArgs e)
    {
        string ID = Hidden1.Value;

        if (!ID.Equals(""))
        {
            Response.Redirect("GoodPic_Edit.aspx?GoodID=" + ID);
        }
    }
}
